package com.audiorecorder;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.MyApplication;
import com.pbase.tools.Logger;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

/**
 * User: psn
 * Date: 2021/10/28
 */
public class AudioPlayer implements MediaPlayer.OnPreparedListener,
        MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener , DefaultLifecycleObserver {

    private MediaPlayer mediaPlayer = null;
    private String path;
    private AudioPlayerCallback callback;


    public interface AudioPlayerCallback {
        void playOK();
    }

    @Override
    public void onPause(@NonNull @NotNull LifecycleOwner owner) {
        Logger.e(getClass(),"onPause");
        stop();
    }

    /**
     * @param path
     * @param callback
     */
    public synchronized void start(String path, AudioPlayerCallback callback) {
        Logger.e(getClass(), "brnum public synchronized void start" + callback);
        if (TextUtils.isEmpty(path)) {
            return;
        }
        this.callback = callback;
        this.path = path;
        try {
            if (mediaPlayer != null) {//有未完成的声音
                if (mediaPlayer.isPlaying()) {
                    stop();
                }
            }
            this.path = null;
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnCompletionListener(this);
            mediaPlayer.setOnPreparedListener(this);

            mediaPlayer.setDataSource(MyApplication.getInstance(), Uri.parse(path));
            mediaPlayer.prepare();
            Logger.e(getClass(), "prepare");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public synchronized void stop() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    //暂停
    public void pause() {
        if (null != mediaPlayer && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    //播放
    public void reStart() {
        if (null != mediaPlayer && !mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }
    }

    // 播放完成
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        stop();
        if (callback != null) {
            callback.playOK();
        }
    }

    // 准备完成
    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        Logger.e(getClass(), "start");
        mediaPlayer.start();
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        Logger.e(getClass(), "onError what " + what + " extra " + extra);
        return false;
    }

    //获取音乐总时长
    public int getDuration() {
        if (mediaPlayer != null) {
            return mediaPlayer.getDuration();
        }
        return -1;
    }

    //获取当前播放的位置
    public int getCurrentPosition() {
        if (mediaPlayer != null) {
            return mediaPlayer.getCurrentPosition();
        }
        return -1;
    }
    //设置播放位置，使用准确定位模式
    public void seekTo(long msec) {
        if (mediaPlayer != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                mediaPlayer.seekTo(msec, MediaPlayer.SEEK_CLOSEST);
            }
        }
    }


}
